<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        function compare(val1, val2) {
            if (val1 < val2) {
                return -1;    //改变return的值从而改变排序
            } else if (val1 > val2) {
                return 1;
            } else {
                return 0;
            }
        }
        // arr2 = [13, 24, 52, 3];
        // console.log(arr2.sort(compare)); //[3,13,24,51]

        function compare(val1, val2) {
            if(val1 < val2) {
                return -1;
            } else if (val1 > val2) {
                return 1;
            } else {
                return 0; 
            }
        }

        arr.sort(compare);
    </script>

</body>

</html>